<script lang="ts">
export default {
  name: 'member'
};
</script>

<script setup lang="ts">
import Edit from './components/edit.vue';
import router from '@/router';
import { formatMillisecondsToDate } from '@/utils/utils'
import {
  getMemberList,
  deleteMember,
  updateStatusMember
} from '@/api/member';
import { getDictPage } from '@/api/dict'
import { aDownloadFile, aDownloadHref } from '@/utils/utils'


const queryFormRef = ref(ElForm); // 查询表单
const formRef = ref(ElForm); // 用户表单
const defaultTime: [Date, Date] = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
];
const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams: any = reactive({
  current: 1,
  pageSize: 10,
  startDate: '',
  endDate: '',
  status: null
});

const formData = reactive<any>({
  status: 1,
  startEndTime: []
});

const dialog = reactive<any>({
  visible: false,
});

const dataList = ref([]);
const statusList = ref<any>([]);


function getDict() {
  getDictPage({
    current: 1,
    pageSize: 9999,
    parentId: 6
  }).then((res) => {
    statusList.value = res.data.data
  })
}

/**
 * 查询
 */
function handleQuery() {
  getMemberList({
    ...queryParams,
    status: Number(queryParams.status)
  })
    .then((res: any) => {

      dataList.value = res.data.data;
      total.value = res.data.total;
    })
}
/**
 * 重置查询
 */
function resetQuery() {
  queryParams.endDate = '';
  queryParams.startDate = '';
  queryParams.current = 1;
  queryFormRef.value.resetFields();
  handleQuery();
}


/**
 * 行checkbox change事件
 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}


/**
 * 重置表单
 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
}


/**
 * 类型表单弹窗
 *
 */
function openDialog(row?: object) {
  dialog.visible = true;
  if (row) {
    dialog.title = '预览';
    Object.assign(formData, row);
    formData.startEndTime = [formData.startDate, formData.endDate]
  } else {
    //resetForm();
    dialog.title = '预览';
  }
}

/**
 * 删除
 */
function handleDelete(id?: number) {
  let idList = ids.value
  id ? idList = [...ids.value, id] : null
  if (idList.length === 0) {
    ElMessage.warning('请勾选删除项');
    return;
  }

  ElMessageBox.confirm('确认删除已选中的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteMember({ ids: idList }).then(() => {
      ElMessage.success('删除成功');
      handleQuery();
    });
  });
}

function changeSwitch(row: any) {
  updateStatusMember({
    ...row
  }).then((res: any) => {
    ElMessage.success('操作成功');
  })
}


onMounted(async () => {
  getDict()
  handleQuery(); // 初始化用户列表数据
});

watch(
  () => queryParams.startEndTime,
  (val: any) => {
    if (Array.isArray(val)) {
      queryParams.startDate = val[0];
      queryParams.endDate = val[1];
    } else {
      queryParams.startDate = '';
      queryParams.endDate = '';
    }
  }
);


</script>

<template>
  <div class="app-container">

    <el-row :gutter="24">
      <el-col :lg="24" :xs="24">
        <div class="search">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">

            <el-form-item prop="username" label="会员名">
              <el-input v-model="queryParams.username" placeholder="请输入" />
            </el-form-item>
            <el-form-item prop="phone" label="手机号">
              <el-input v-model="queryParams.phone" placeholder="请输入" />
            </el-form-item>
            <el-form-item prop="status" label="状态">
              <el-select v-model="queryParams.status" placeholder="请选择">
                <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <i-ep-search />搜索
              </el-button>
              <el-button @click="resetQuery">
                <i-ep-refresh />
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="never">
          <template #header>
            <div class="flex justify-between">
              <div>
                <el-button type="success" @click="openDialog()">
                  <i-ep-plus />新增
                </el-button>
                <el-button type="danger" :disabled="ids.length === 0" @click="handleDelete()">
                  <i-ep-delete />删除
                </el-button>
              </div>
            </div>
          </template>

          <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" type="index" align="center" width="55" />
            <el-table-column label="会员名" align="center" prop="username" width="200" />
            <el-table-column label="昵称" align="center" prop="nickname" />
            <el-table-column label="手机号" align="center" prop="phone" />
            <el-table-column label="注册时间" align="center" prop="createTime" width="120" />
            <el-table-column label="状态" align="center" prop="status" width="120">
              <template #default="scope">
                <el-switch v-model="scope.row.status" :inactive-value="-1" :active-value="1"
                  @click="changeSwitch(scope.row)" disabled />
              </template>
            </el-table-column>
            <el-table-column label="头像" align="center" prop="status" width="120">
              <template #default="scope">
                <img class="column-img" :src="scope.row.icon" alt="">
              </template>
            </el-table-column>
            <el-table-column label="性别" align="center" prop="gender" width="200" />
            <!-- <el-table-column label="生日" align="center" prop="birthday" /> -->
            <el-table-column label="城市" align="center" prop="city" />
            <el-table-column label="个性签名" align="center" prop="personalizedSignature" />
            <el-table-column label="性别" align="center" prop="gender" width="200" />
            <el-table-column label="用户来源" align="center" prop="sourceType" />
            <el-table-column label="积分" align="center" prop="integration" />
            <el-table-column label="成长值" align="center" prop="growth" />
            <el-table-column label="剩余抽奖次数" align="center" prop="luckeyCount" />
            <el-table-column label="历史积分数量" align="center" prop="historyIntegration" />
            <el-table-column label="操作" fixed="right" align="center" width="250">
              <template #default="scope">
                <el-button type="primary" link size="small" @click.stop="handleDelete(scope.row.id)">
                  <i-ep-delete />删除
                </el-button>
                <!-- <el-button type="primary" link size="small" @click.stop="openDialog(scope.row)">
                  <i-ep-edit />编辑
                </el-button> -->
              </template>
            </el-table-column>
          </el-table>
          <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.current"
            v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
        </el-card>
      </el-col>
    </el-row>
    <Edit :dialog="dialog" :formData="formData" @resetQuery="resetQuery"></Edit>
  </div>
</template>
<style lang="scss" scoped>
.column-img {
  width: 100px;
  height: 100px;

}
</style>